﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class admin_Ticker : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadData();
            LoadTicker();
        }
        Utilities.SetLabel(Master, "Ticker");
    }

    private void LoadTicker()
    {
        string lang_id = ddl_language.SelectedValue;

        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from Fn_GetTicker(@lang_id)", conn))
            {
                cmd.Parameters.Add(new SqlParameter("@lang_id", lang_id));
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    if (dr.Read())
                    {
                        tbticker.Text = dr["text"].ToString();
                        hf_id.Value = dr["id"].ToString();
                    }
                    else
                    {
                        tbticker.Text = "";
                        hf_id.Value = "";
                    }
                }
            }
        }
    }

    private void LoadData()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllLanguages(0, 1000000, 'id', 'asc')", conn))
            {
                SqlDataReader dr = null;

                dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
                            ddl_language.Items.Add(new ListItem(dr["language_name"].ToString(), dr["id"].ToString()));
                        }
                    }
                }
                dr.Close();
            }
        }
    }
    protected void ddl_language_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadTicker();
    }
    protected void btn_save_Click(object sender, EventArgs e)
    {
        if (hf_id.Value != "")
        {
            int id;
            Int32.TryParse(hf_id.Value, out id);
            if (id > 0)
            {
                //update
                using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("SP_UpdateTicker", conn))
                    {
                        SqlParameter[] pars = new SqlParameter[2];
                        pars[0] = new SqlParameter("text", tbticker.Text);
                        pars[1] = new SqlParameter("id", id);
                        cmd.CommandType = System.Data.CommandType.StoredProcedure;
                        foreach (SqlParameter s in pars)
                            cmd.Parameters.Add(s);
                        try
                        {
                            object val = cmd.ExecuteScalar();
                            Session["message"] = " Insert was succesfull<br />";
                            Session["message_type_css"] = "par_message_ok";
                        }
                        catch (Exception ex)
                        {
                            Session["message"] += " There was an error inserting a new page " + ex.ToString() + "<br />";
                            Session["message_type_css"] = "par_message_error";
                        }
                        cmd.Parameters.Clear();
                    }
                }
            }
        }
        else
        {
            //update
            using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SP_InsTicker", conn))
                {
                    SqlParameter[] pars = new SqlParameter[2];
                    pars[0] = new SqlParameter("text", tbticker.Text);
                    pars[1] = new SqlParameter("lang_id", ddl_language.SelectedValue);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;
                    foreach (SqlParameter s in pars)
                        cmd.Parameters.Add(s);
                    try
                    {
                        object val = cmd.ExecuteScalar();
                        Session["message"] = " Insert was succesfull<br />";
                        Session["message_type_css"] = "par_message_ok";
                    }
                    catch (Exception ex)
                    {
                        Session["message"] += " There was an error inserting a new page " + ex.ToString() + "<br />";
                        Session["message_type_css"] = "par_message_error";
                    }
                    cmd.Parameters.Clear();
                }
            }
        }
        Response.Redirect(Request.Url.ToString());
    }
}
